// import Vue from 'vue';
import { createI18n } from 'vue-i18n';
import Storage from '../utils/Storage.js';
import eEn from 'element-plus/es/locale/lang/en'; // element-plus lang
import eZh from 'element-plus/es/locale/lang/zh-cn'; // element-plus lang
import en from './en.js';
import zh from './zh.js';


String.prototype.format = function() {
    const args = arguments;
    return this.replace(/{(\d+)}/g, function(match, number) {
        return typeof args[number] != 'undefined' ? args[number] : match;
    });
};

const messages = { en: { ...en, ...eEn }, zh: { ...zh, ...eZh } };

export function getLanguage() {
    const storedLang = Storage.getLocalStorage('I18N').get('LOCALE');
    if (storedLang) return storedLang;

    // if has not choose language
    const language = (navigator.language || navigator.browserLanguage).toLowerCase();
    const locales = Object.keys(messages);
    for (const locale of locales) {
        if (language.indexOf(locale) > -1) {
            return locale;
        }
    }
    return 'zh';
}

const i18n = createI18n({
    locale: getLanguage(),
    globalInjection: true,
    legacy: false,
    messages
});

export default i18n;
